package chapter03
import org.apache.spark.{SparkConf, SparkContext}
object Test08_MapPartitionsWithIndex {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[*]").setAppName("MapPartitionsWithIndex")
    val sc = new SparkContext(conf)
    //带分区序号的算子
    val value = sc.makeRDD(List(1, 2, 3, 4, 5, 6, 7, 8, 9), 3)
    //将每个分区中的元素都加上当前的分区序号
    val value1 = value.mapPartitionsWithIndex(
      (index, e) => e.map(f => f + index))
    println(value1.collect().mkString("Array(", ", ", ")"))
    //返回指定分区的数据 返回第二个分区的数据 下标从0开始
    val value2 = value.mapPartitionsWithIndex(
      (index, e) => {
        if (index == 1) e else Iterator()
      }
    )
    println(value2.collect().mkString("Array(", ", ", ")"))
  }
}
